<?php

define('OPINIONS', 'opinions');
define('WHITEBOARD', 'whiteboard');

define('FRIEND', 'friend');
define('FRIEND_PENDING', 'friend_pending');

define('FOLLOWING', 'following');
define('FOLLOWER', 'follower');

define('FAVORITE', 'favorite');
define('FAVORITOR', 'favoritor');

define('OPINIONS_FRIEND', json_encode(array(
            'app' => OPINIONS,
            'relationship' => FRIEND
        )));
define('OPINIONS_FRIEND_PENDING', json_encode(array(
            'app' => OPINIONS,
            'relationship' => FRIEND_PENDING
        )));

define('OPINIONS_FOLLOWING', json_encode(array(
            'app' => OPINIONS,
            'relationship' => FOLLOWING
        )));
define('OPINIONS_FOLLOWER', json_encode(array(
            'app' => OPINIONS,
            'relationship' => FOLLOWER
        )));

define('WHITEBOARD_FOLLOWING', json_encode(array(
            'app' => WHITEBOARD,
            'relationship' => FOLLOWING
        )));
define('WHITEBOARD_FOLLOWER', json_encode(array(
            'app' => WHITEBOARD,
            'relationship' => FOLLOWER
        )));
define('OPINIONS_FAVORITE', json_encode(array(
            'app' => OPINIONS,
            'relationship' => FAVORITE
        )));
define('OPINIONS_FAVORITOR', json_encode(array(
            'app' => OPINIONS,
            'relationship' => FAVORITOR
        )));

define('WHITEBOARD_FAVORITE', json_encode(array(
            'app' => WHITEBOARD,
            'relationship' => FAVORITE
        )));
define('WHITEBOARD_FAVORITOR', json_encode(array(
            'app' => WHITEBOARD,
            'relationship' => FAVORITOR
        )));

define('WHITEBOARD_FRIEND', json_encode(array(
            'app' => WHITEBOARD,
            'relationship' => FRIEND
        )));
define('WHITEBOARD_FRIEND_PENDING', json_encode(array(
            'app' => WHITEBOARD,
            'relationship' => FRIEND_PENDING
        )));

function greengar_remove_entity_relationship($guid_one, $relationship, $guid_two) {
    global $CONFIG;

    $guid_one = (int) $guid_one;
    $guid_two = (int) $guid_two;

    $obj = check_entity_relationship($guid_one, $relationship, $guid_two);
    if ($obj == false) {
        return false;
    }

    return delete_data("DELETE from {$CONFIG->dbprefix}entity_relationships where guid_one=$guid_one and relationship='$relationship' and guid_two=$guid_two");
}

//
//function greengar_check_entity_relationship($guid_one, $relationship, $guid_two) {
//	global $CONFIG;
//
//	$guid_one = (int)$guid_one;
//	// $relationship = sanitise_string($relationship);
//	$guid_two = (int)$guid_two;
//
//	if ($row = get_data_row("SELECT * FROM {$CONFIG->dbprefix}entity_relationships WHERE guid_one=$guid_one AND relationship='$relationship' AND guid_two=$guid_two limit 1")) {
//		return $row;
//	}
//
//	return false;
//}

function greengar_get_relationship($app, $pending = false) {
    switch ($app) {
        case OPINIONS:
            return $pending ? OPINIONS_FRIEND_PENDING : OPINIONS_FRIEND;
        case WHITEBOARD:
            return $pending ? WHITEBOARD_FRIEND_PENDING : WHITEBOARD_FRIEND;
    }
}

function greengar_get_relationship_following($app, $following = false) {
    switch ($app) {
        case OPINIONS:
            return $following ? OPINIONS_FOLLOWING : OPINIONS_FOLLOWER;
        case WHITEBOARD:
            return $following ? WHITEBOARD_FOLLOWING : WHITEBOARD_FOLLOWER;
    }
}

function greengar_get_relationship_favorite($app, $favorite = false) {
    switch ($app) {
        case OPINIONS:
            return $favorite ? OPINIONS_FAVORITE : OPINIONS_FAVORITOR;
        case WHITEBOARD:
            return $favorite ? WHITEBOARD_FAVORITE : WHITEBOARD_FAVORITOR;
    }
}

function greengar_check_relationship($guid_one, $guid_two, $app, $pending = false) {
    $relationship = greengar_get_relationship($app, $pending);
//    echo $relationship;
    try {
        return check_entity_relationship($guid_one, $relationship, $guid_two);
    } catch (DatabaseException $de) {

    }
}

function greengar_add_relationship($guid_one, $guid_two, $app, $pending = false) {
    $relationship = greengar_get_relationship($app, $pending);
//    echo $relationship;
    try {
        return add_entity_relationship($guid_one, $relationship, $guid_two);
    } catch (DatabaseException $de) {
        
    }
}

function greengar_remove_relationship($guid_one, $guid_two, $app, $pending = false) {
    $relationship = greengar_get_relationship($app, $pending);
//    echo $relationship;
    try {
        return greengar_remove_entity_relationship($guid_one, $relationship, $guid_two);
    } catch (DatabaseException $de) {

    }
}

function greengar_check_relationship_following($guid_one, $guid_two, $app, $following = false) {
    $relationship = greengar_get_relationship_following($app, $following);
//    echo $relationship;
    try {
        return check_entity_relationship($guid_one, $relationship, $guid_two);
    } catch (DatabaseException $de) {

    }
}

function greengar_add_relationship_following($guid_one, $guid_two, $app, $following = false) {
    $relationship = greengar_get_relationship_following($app, $following);
//    echo $relationship;
    try {
        return add_entity_relationship($guid_one, $relationship, $guid_two);
    } catch (DatabaseException $de) {
        
    }
}

function greengar_remove_relationship_following($guid_one, $guid_two, $app, $following = false) {
    $relationship = greengar_get_relationship_following($app, $following);
//    echo $relationship;
    try {
        return greengar_remove_entity_relationship($guid_one, $relationship, $guid_two);
    } catch (DatabaseException $de) {

    }
}

function greengar_check_relationship_favorite($guid_one, $guid_two, $app, $favorite = true) {
    $relationship = greengar_get_relationship_favorite($app, $favorite);
//    echo $relationship;
    try {
        return check_entity_relationship($guid_one, $relationship, $guid_two);
    } catch (DatabaseException $de) {

    }
}

function greengar_add_relationship_favorite($guid_one, $guid_two, $app, $favorite = true) {
    $relationship = greengar_get_relationship_favorite($app, $favorite);
//    echo $relationship;
    try {
//        if($favorite) {
//            $entity_two = get_entity($guid_two);
//            $entity_two->likenum = $entity_two->likenum + 1;
//            $entity_two->save();
//        }
        return add_entity_relationship($guid_one, $relationship, $guid_two);
    } catch (DatabaseException $de) {
        
    }
}

function greengar_remove_relationship_favorite($guid_one, $guid_two, $app, $favorite = true) {
    $relationship = greengar_get_relationship_favorite($app, $favorite);
//    echo $relationship;
    try {
//        if($favorite) {
//            $entity_two = get_entity($guid_two);
//            $entity_two->likenum = $entity_two->likenum - 1;
//            $entity_two->save();
//        }
        return greengar_remove_entity_relationship($guid_one, $relationship, $guid_two);
    } catch (DatabaseException $de) {

    }
}

?>